﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.Site.BackEnd.ApCode.Filters;


namespace GTAuto.Site.BackEnd.Controllers
{

    [AutoSection]
    public class HomeController : AdminBaseController
    {
        //
        // GET: /Home/

        public  ActionResult Test(string id)
        {
            Graphics graphicsObj;
            Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("\\Uploads\\BrendLogos\\marka_1.jpg"));
            Graphics g = Graphics.FromImage(newImage1);

            int w = Convert.ToInt32(id.Split('_')[0]);
            int h = Convert.ToInt32(id.Split('_')[1]);

            Size size = new Size();
            size.Width = w;
            size.Height = h;
            newImage1 = ResizeImage(newImage1, size);
            var temp = ImageToByteArray(newImage1);

            return File(temp, "image/jpeg", "fileName.jpg");
        }


        public ActionResult Index()
        {
            return RedirectToAction("Brands", "Dictionary");

            //Graphics graphicsObj;
            //Image newImage1 = Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("\\Uploads\\BrendLogos\\marka_1.jpg"));
            //Graphics g = Graphics.FromImage(newImage1);

            //Size size = new Size();
            //size.Width = 180;
            //size.Height = 154;
            //newImage1 = ResizeImage(newImage1, size);
            //var temp =  ImageToByteArray(newImage1);

            //return File(temp, "image/jpeg", "fileName.jpg");
            return View();
        }


        public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            var ms = new MemoryStream();
            imageIn.Save(ms, ImageFormat.Png);
            return ms.ToArray();
        }

        protected static Image ResizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }
        

    }
}
